package com.chen.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.common.Result;
import com.chen.context.SearchStrategyContext;
import com.chen.dto.ArticleSearchDTO;
import com.chen.mapper.ArticleMapper;
import com.chen.mapper.DictMapper;
import com.chen.mapper.UserMapper;
import com.chen.pojo.Article;
import com.chen.pojo.Dict;
import com.chen.pojo.User;
import com.chen.service.ArticleService;
import com.chen.vo.ConditionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author chenchao

 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private DictMapper dictMapper;

    @Autowired
    private SearchStrategyContext searchStrategyContext;

    private static final String DICT = "dict";


    @Autowired
    private RedisTemplate redisTemplate;




    /**
     * 撰写博客
     *
     * @param article
     * @return
     */
        public Result<?> addArticle(Article article) {
            article.setTime(new Date());
        if (StrUtil.isBlank(article.getImg())) {
            article.setImg("https://static.talkxj.com/articles/7cdd5d97ce85a65988f510ff79c90e46.jpg");
        }
        Dict dict = new Dict();
        dict.setContent(article.getCategory());

        List<Dict> dictList = dictMapper.getDict();
        boolean b = dictList.stream().anyMatch(s -> article.getCategory().equals(s.getContent()));
        if (!b){
            dictMapper.addDict(dict);
            flush(DICT);
        }

        return Result.success(articleMapper.insert(article));
    }


    /**
     * 编辑博客
     *
     * @param article
     * @return
     */
    public Result<?> updateNews(Article article) {
        article.setUtime(new Date());
        return Result.success(articleMapper.updateById(article));
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public Result<?> getById(Long id) {
        return Result.success(articleMapper.selectById(id));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public Result<?> delete(Long id) {
        articleMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 查询所有
     *
     * @return
     */
    public Result<?> findAll() {
        User user = new User();
        List<Article> article = articleMapper.selectList(null);
        for (Article article1 : article) {
            User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, article1.getAuthor()));
            if (ObjectUtil.isNotEmpty(res)) {
                if (StrUtil.isNotBlank(res.getAvatar())) {
                    article1.setAvatar(res.getAvatar());
                } else {
                    // 默认一个头像
                    article1.setAvatar("https://img2.baidu.com/it/u=2906052251,1830965798&fm=26&fmt=auto");
                }
                if (StrUtil.isNotBlank(res.getNick())) {
                    article1.setNick(res.getNick());
                } else {
                    // 默认一个头像
                    article1.setAvatar("ヅ記憶丶锝浅笑 ");
                }
            }
        }
        return Result.success(article);
    }

    /**
     * 查询前五条数据
     *
     * @return
     */
    public Result<?> articleList() {
        return Result.success(articleMapper.articleLis());
    }

    /**
     * 查询这一个用户的信息
     *
     * @return
     */
    public Result<?> list1(String author) {
        return Result.success(articleMapper.list(author));
    }

    public Result<?> categoryList(String author){
        return Result.success(articleMapper.categoryList(author));
    }


    public Result<?> categorys(String author,String category){
        return Result.success(articleMapper.categorys(author,category));
    }


    public Result  updateCount(Article article){
        return Result.success(articleMapper.likerCounts(article));
    }

    public Result  selectArticleOne(int id){
        return Result.success(articleMapper.selectArticleOne(id));
    }

    public Result  selectArticleByFlag(){

        User user = new User();
        List<Article> article = articleMapper.selectArticleByFlag();
        for (Article article1 : article) {
            User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, article1.getAuthor()));
            if (ObjectUtil.isNotEmpty(res)) {
                if (StrUtil.isNotBlank(res.getAvatar())) {
                    article1.setAvatar(res.getAvatar());
                } else {
                    // 默认一个头像
                    article1.setAvatar("https://img2.baidu.com/it/u=2906052251,1830965798&fm=26&fmt=auto");
                }
                if (StrUtil.isNotBlank(res.getNick())) {
                    article1.setNick(res.getNick());
                } else {
                    // 默认一个头像
                    article1.setAvatar("ヅ記憶丶锝浅笑 ");
                }
            }
        }
        return Result.success(article);
    }

    public Result seeCount(){
        return Result.success(articleMapper.seeCount());
    }



    public Article getByIds(Long id) {
        return articleMapper.selectById(id);
    }

    @Override
    public List<ArticleSearchDTO> listArticlesBySearch(ConditionVO condition) {
        return searchStrategyContext.executeSearchStrategy(condition.getKeywords());
    }

    @Override
    public List<Article> isEnable() {

        return articleMapper.isEnable();
    }


    public void flush(String key) {
        redisTemplate.delete(key);
    }
}
